/*
 * Created on 2005-7-6
 * Last modified on 2007-6-6
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.html;

import java.text.ParseException;

import org.apache.log4j.Logger;

import com.yeqiangwei.util.FormatDateTime;
import com.yeqiangwei.util.Validator;

/**
 * @author YeQiangWei by 我容易么我
 * Powered by YeQiangWei.com
 */
public class OutPrint {
	
	private static final Logger logger = Logger.getLogger(OutPrint.class);
	
	public static String isDateHtml(long l){
		return isDateHtml(FormatDateTime.decodeTime(l));
	}
	
   
   /**
    * 定制页面时间输出格式
    * @param sb
    * @return
    * com.yeqiangwei.html
    */
    public static String isDateHtml(String sb){
        if(Validator.isEmpty(sb)){
            return "";
        }
        else if(sb.length()==14){
        	sb = FormatDateTime.decodeTime(sb);
        }
        String d = "";
        String t = "";
        sb = sb.replace(".0","");
        d = sb.substring(0, sb.indexOf(" ")); /*取年月*/
        t = sb.substring(sb.indexOf(" ")+1, sb.length()-3); /*取时间去掉秒数*/
        String ts=""; 
        boolean isAfter;
        try{
        	String now = FormatDateTime.formatDateTime("yyyy-MM-dd");
        	isAfter = FormatDateTime.dateCompare(d, now);
            if(!isAfter){ /* 今天 */
            	d = d.substring(2, d.length());
                ts=("<font style=\"font-size:12px; color:#cc0000;font-weight:bold;\">今天</font> ");
                ts+=(" <font style=\"font-size:12px; color:#990066\">"+t);  
                ts+=("</font>"); 
             }else{
            	 /* 计算昨天 */
            	 ts = FormatDateTime.dateAdd(FormatDateTime.DAY_OF_MONTH,-1); /* 返回间隔一天后的时候 */
            	 ts = ts.substring(0, 11); /* 取年月日 */
            	 isAfter = FormatDateTime.dateCompare(d, ts);
            	 if(!isAfter){
                    d = d.substring(2, d.length());
                    ts=("<font style=\"font-size:12px; color:#006600;font-weight: bold;\">昨天</font> ");
                    ts+=(" <font style=\"font-size:12px; color:#990066\">"+t);  
                    ts+=("</font>");  
            	 }else{
                   	 /* 计算前天 */
                	 ts = FormatDateTime.dateAdd(FormatDateTime.DAY_OF_MONTH,-2); /* 返回间隔两天后的时候 */
                	 ts = ts.substring(0, 11); /* 取年月日 */
                	 isAfter = FormatDateTime.dateCompare(d, ts);
                	 if(!isAfter){
                        d = d.substring(2, d.length());
                        ts=("<font style=\"font-size:12px; color:#2A00FF;font-weight: bold;\">前天</font> ");
                        ts+=(" <font style=\"font-size:12px; color:#990066\">"+t);  
                        ts+=("</font>");  
                	 }else{
                		d = d.substring(2, d.length());
                		ts=(d+ " <font style=\"color:#990066\">" + t);
         				ts+=("</font>");
                	 }
            	 }
            }
        }catch(Exception e){
        	logger.error(e.toString());
        }
        return ts;       
    }
    
    public static String isDateHtml2(String sb, String format){
        if(Validator.isEmpty(sb)){
            return "";
        }
        String d = "";
        String t = "";
        sb = sb.replace(".0","");
        d = sb.substring(0, sb.indexOf(" "));
        t = sb.substring(sb.indexOf(" ")+1, sb.length());
        String ts=""; 
        boolean isAfter;
        try{
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(format);
            String isDate = sdf.format(new java.util.Date());   
            java.util.Date date1 ;
            java.util.Date date0 ;
            ts= (d);
            date1 = sdf.parse(ts);
            date0 = sdf.parse(isDate);
            isAfter = date0.after(date1);   
            if(!isAfter){
                ts=("<span style=\"font-size: 12px; color:#cc0000;font-weight: bold;\">今天</span> ");
                ts+=(" <span style=\"font-size:12px;color:#990066\">"+t);  
                ts=(ts.substring(0,(ts.length()-3)));
                ts+=("</span>");                
            }else{
               ts+=(" <span style=\"font-size:12px; color:#990066\">"+t);
               ts=(ts.substring(2,(ts.length()-3)));
               ts+=("</span>");
            }
            //this.isAfter = isAfter;
        }catch(ParseException e){
            
        }
        return ts;       
    }
    
    /**
     * 不显示时间
     * @param sb
     * @param format
     * @return
     * 2005-11-7 6:29:48 Made In YeQiangWei
     * com.yeqiangwei.html
     */
    public static String isDateHtml3(String sb, String format){
    	if(Validator.isEmpty(sb)){
            return "";
        }
        String d = "";
        sb = sb.replace(".0","");
        d = sb.substring(0, sb.indexOf(" "));
        //t = sb.substring(sb.indexOf(" ")+1, sb.length());
        String ts=""; 
        boolean isAfter;
        try{
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(format);
            String isDate = sdf.format(new java.util.Date());   
            java.util.Date date1 ;
            java.util.Date date0 ;
            ts= (d);
            date1 = sdf.parse(ts);
            date0 = sdf.parse(isDate);
            isAfter = date0.after(date1);   
            if(!isAfter){
                ts=("<span style=\"font-size: 12px;color:#cc0000;font-weight: bold;\">今天</span> ");
            }else{
                ts=d;
            }
        }catch(ParseException e){
            
        }
        return ts;       
    }
    
    public static int getLastpage(long total,int rows){
    	int lastpage = 1;
        if(total%rows!=0){
        	lastpage = (int) (total/rows + 1);
        }else{
        	lastpage = (int) (total/rows);
        }
        if(lastpage<1){lastpage=1;}
        return lastpage;
    }
    
    public static String pagination(int page, int rows, long total, String url, int num){
    	return OutPrint.pagination(page,rows,total,url,num,0);
    }
    
    /**
     * 
     * @param page 当前页
     * @param pageNum 每页显示条数
     * @param total 记录总数
     * @param url 如：thread.jsp?
     * @param num 每多少页为一组
     * @return
     */
	public static String pagination(int page, int rows, long total, String url, int num, int maxpage){
		StringBuffer sb = new StringBuffer();
		if((page<1||rows<1||total<1)||total<=rows){
			logger.debug("page<1||rows<1||total<1)||total<=rows");
			return "";
		}
        int start = 1, end = num;
        int pages = getLastpage(total,rows);
        /* 当前页超过总页数 */
        if(page > pages){
        	page = pages;
        	end = pages;
        	start = end - num - 1; 
        }
        /* 当前页大于指定显示的跳转页数 */
        if(page>=num){
            end = page + num;
            start = page - (num - 2);
            if(end>pages){
            	end = pages;
            }
        }
        sb.append("<div class=\"pagination\"><ul>");
        sb.append("<li class=\"ppg\">");
        sb.append(total);
        sb.append("</li>");
        sb.append("<li class=\"ppg\">");
        sb.append(page);
        sb.append("/");
        sb.append(pages);
        sb.append("</li>");
        if(pages-start<num && page>num){
        	start = pages - (num - 2);
        }
    	if(end >= pages){
    		end = pages;
    		start = end - num -1;
    		if(start<=0){
    			start = 1;
    		}
    	}
    	if(start>2){
        	sb.append("<li class=\"png\">");
        	sb.append("<a href=\"");
            sb.append(url);
            sb.append("1");
            sb.append("&lpage=");
            sb.append(pages);
            sb.append("\" title=\"跳转到第一页\">1...</a></li>");
    	}else if(start==2){
        	sb.append("<li>");
        	sb.append("<a href=\"");
            sb.append(url);
            sb.append("1");
            sb.append("&lpage=");
            sb.append(pages);
            sb.append("\" title=\"跳转到第一页\">1</a></li>");
            
    	}
    	if(maxpage>0&&end>maxpage)end=maxpage;
        for(int i=start; i<=end; i++){
        	sb.append("<li");
        	if(i==page){
        		sb.append(" class=\"pcg\">");
        		sb.append(i);
        		sb.append("</li>");
        	}else{
	        	sb.append(">");
	        	sb.append("<a href=\"");
	            sb.append(url);
	            sb.append(i);
	            sb.append("&lpage=");
	            sb.append(pages);
	            sb.append("\"");
	            sb.append(">");
	            sb.append(i);
	            sb.append("</a>");
	            sb.append("</li>");
        	}
        }
        /* 不提供最后一页 
        if(end<pages){
        	sb.append("<li><a href=\"");
            sb.append(url);
            sb.append(pages);
            sb.append("&lpage=");
            sb.append(pages);
            sb.append("\">...");
            sb.append(pages);
        	sb.append("</a></li>");
        }*/
        if(page<pages){
        	sb.append("<li class=\"png\">");
        	sb.append("<a href=\"");
            sb.append(url);
            sb.append(page+1);
            sb.append("&lpage=");
            sb.append(pages);
            sb.append("\">");
        	sb.append("下一页</a></li>");
        }
        sb.append("</ul></div>");
		return sb.toString();
	}
    
}
